package com.hw.one.collect.radar.config;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/07/05 15:24.
 */
public class RadarConfig {
    private static Logger log = Logger.getLogger(RadarConfig.class);

    private static Properties properties = new Properties();

    private RadarConfig(){}

    private static RadarConfig config;

    /**
     * 获取认证实例
     * @return
     */
    public static RadarConfig getInstance(){
        if(null == config){
            config = new RadarConfig();
            initConfig();
        }
        return config;

    }
    /**
     * 初始化认证信息
     */
    private static void initConfig(){
        initDefault();
    }

    private static void initDefault(){
        String defaultSrc = "/config/radar.properties";
        InputStream is = RadarConfig.class.getResourceAsStream(defaultSrc);
        try {
            log.debug("conf配置文件流：" + is);
            if(is != null){
                properties.load(is);
            }
        } catch (IOException e) {
            log.error("加载默认配置文件",e);
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 通过key获取value
     * @return
     */
    public String get(String key){
        String value = properties.getProperty(key);
        log.debug("资源：{"+key+":"+value+"}");
        return value;
    }

    public Double getDouble(String key) {
        return Double.valueOf(get(key));
    }
}
